package view;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.util.Hashtable;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import control.MainWindowController;

/**
 * Container Class for all UI informations and actions.
 * 
 * @author Joao Carlos, Jody Matos
 * 
 * @author 	Jody Matos, Alenilson
 * @version 1.2
 * @since	12/15/2011
 * 
 * <dt><b>History:</b></dt>
 * 	<dd>1.0: Creation of Raw window.</dd>
 *  <dd>1.1: Applying the <code>GroupLayout</code>.</dd>
 *  <dd>1.2: Inserting buttons and the <code>FielPanel</code>.</dd>
 */
@SuppressWarnings("serial")
public class MainWindow extends JFrame{
	private Dimension		screenSize;
	private FieldPanel 		fieldPanel;
	private FunctionalPanel		functionalPanel;
	private JPanel		statusPanel;
	private static int	borderGap = 10;
	private static int  innerGap = 5;
	
	public MainWindow(MainWindowController controller) {
		super("QPRS - Quadtree Path Robotic Simulator");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setExtendedState(JFrame.MAXIMIZED_BOTH);
		this.setResizable(false);
	
		
		screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		
		fieldPanel = new FieldPanel();
		fieldPanel.addMouseMotionListener(controller);
		fieldPanel.addMouseListener(controller);
		
		functionalPanel = new FunctionalPanel(controller);
		functionalPanel.setBorder(BorderFactory.createEtchedBorder());
		
		statusPanel = new JPanel();
		statusPanel.setBorder(BorderFactory.createEtchedBorder());
		
		GroupLayout layout = new GroupLayout(this.getContentPane());
		this.getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createSequentialGroup()
				.addComponent(functionalPanel, GroupLayout.PREFERRED_SIZE, 250, GroupLayout.PREFERRED_SIZE)
				.addGroup(layout.createParallelGroup()
						.addComponent(fieldPanel, GroupLayout.PREFERRED_SIZE, 600, GroupLayout.PREFERRED_SIZE)
						.addComponent(statusPanel, GroupLayout.PREFERRED_SIZE, screenSize.height-(2*borderGap+innerGap)-50-100, GroupLayout.PREFERRED_SIZE)));
		layout.setVerticalGroup(layout.createParallelGroup()
				.addComponent(functionalPanel, GroupLayout.PREFERRED_SIZE, screenSize.height-2*borderGap-50, GroupLayout.PREFERRED_SIZE)
				.addGroup(layout.createSequentialGroup()
						.addComponent(fieldPanel, GroupLayout.PREFERRED_SIZE, 600, GroupLayout.PREFERRED_SIZE)
						.addComponent(statusPanel, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)));
		
		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);
		
		this.pack();
		
		this.setSize(new Dimension(screenSize.height+100+borderGap, screenSize.height-50));
		this.setLocation(screenSize.width/2-this.getWidth()/2, screenSize.height/2-this.getHeight()/2);
		//this.add(fieldPanel);
	}
	
	public JButton getObstaclesButton(){
		return functionalPanel.getObstaclesButton();
	}
	
	public JButton getPointsButton(){
		return functionalPanel.getPointsButton();
	}
	
	public JPanel getFieldPanel(){
		return this.fieldPanel;
	}
	
	public void mousePressed(int x, int y){
			fieldPanel.mousePressed(x, y);
	}
	
	public void setClicked(boolean clicked){
		fieldPanel.setClicked(clicked);
		fieldPanel.setWidth(0);
		fieldPanel.setHeight(0);
	}
	
	public int getRectX(){
		return fieldPanel.getRectX();
	}
	
	public int getRectY(){
		return fieldPanel.getRectY();
	}
	
	public int getRectWidth(){
		return fieldPanel.getRectWidth();
	}
	
	public int getRectHeight(){
		return fieldPanel.getRectHeight();
	}
	
	public void paintRects(Rectangle rect){
		fieldPanel.paintRects(rect);
	}
	
	public void paintStartPoint(Point startPoint){
		fieldPanel.paintStartPoint(startPoint);
	}
	
	public void paintEndPoint(Point endPoint){
		fieldPanel.paintEndPoint(endPoint);
	}
	
	public JButton getQuadtreeButton() {
		return functionalPanel.getQuadtreeButton();
	}
	
	public JButton getRouteButton(){
		return functionalPanel.getRouteButton();
	}
	
	public JButton getSendButton(){
		return functionalPanel.getSendButton();
	}
	
	public JButton getStartButton(){
		return functionalPanel.getStartButton();
	}
	
	@SuppressWarnings("rawtypes")
	public void paintQuadtreeCells(Hashtable leafNodes){
		fieldPanel.paintQuadtreeCells(leafNodes);
	}
	
	public void setMessageLabel(String message){
		functionalPanel.setMessageLabel(message);
	}
}
